import { UserProfilesAPI } from '@/request/api';

export default{
    namespaced:true,
    state:{
        userInfo:{
            // 昵称
            nickName:'游客1',
            // 积分 鸡腿数
            coin:'--',
            // 头像
            headImg:require('../../assets/img/userImg.f8bbec5e.png')
        },
        // 购物车数量
        cartTotal:0
    },
    mutations:{
        changeUserInfoAndCartTotal(state,payload){
            state.userInfo = payload.userInfo;
            state.cartTotal = payload.cartTotal
        },

        // 删除token后初始化用户信息
        initUserInfo(state){
            state.cartTotal = 0;
            state.userInfo =  {
                nickName: "游客1",
                headImg: require("../../assets/img/userImg.f8bbec5e.png"),
                coin: "--"
            }
        }
    },
    actions:{
        async asyncChangeUserInfoAndCartTotal(context){
            // 请求用户信息
            let user = await UserProfilesAPI()

            // 调用mutations
            context.commit('changeUserInfoAndCartTotal',user.data)
        }
    }
}